package com.bhs.sansong.ui.findLogistics;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;


import com.bhs.sansong.R;
import com.bhs.sansong.adapter.FragmentAdapter;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.CompanyInfo;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.view.CircleProgressView;
import com.bhs.sansong.view.SelectedFont;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 公司评价
 */
public class CompanyEvaluationFragment extends BaseFragment {

    private TabLayout mTabLayout;
    private ViewPager2 mViewPage;
    private final List<TabBean> tabBeanList = new ArrayList<>();
    private int logistics_id;
    private TextView mTvScore;
    private TextView mTvScoreGrade;
    private CircleProgressView mPvService;
    private TextView mTvService;
    private CircleProgressView mPvWaybill;
    private TextView mTvWaybill;
    private CircleProgressView mPvTransport;
    private TextView mTvTransport;
    private CircleProgressView mPvReceipt;
    private TextView mTvReceipt;

    public static CompanyEvaluationFragment newInstance(int logistics_id) {
        CompanyEvaluationFragment fragment = new CompanyEvaluationFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("logistics_id", logistics_id);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public int initLayout() {
        return R.layout.fragment_company_evaluation;
    }

    @Override
    public void initView(View root) {
        logistics_id = getArguments().getInt("logistics_id");
        mTabLayout = root.findViewById(R.id.tabLayout);
        mViewPage = root.findViewById(R.id.view_page);
        tabBeanList.add(new TabBean("全部", "0"));
        tabBeanList.add(new TabBean("好评", "1"));
        tabBeanList.add(new TabBean("中评", "2"));
        tabBeanList.add(new TabBean("差评", "3"));

        mTvScore = root.findViewById(R.id.tv_score);
        mTvScoreGrade = root.findViewById(R.id.tv_score_grade);
        mPvService = root.findViewById(R.id.pv_service);
        mTvService = root.findViewById(R.id.tv_service);
        mPvWaybill = root.findViewById(R.id.pv_waybill);
        mTvWaybill = root.findViewById(R.id.tv_waybill);
        mPvTransport = root.findViewById(R.id.pv_transport);
        mTvTransport = root.findViewById(R.id.tv_transport);
        mTvReceipt = root.findViewById(R.id.tv_receipt);
        mPvReceipt = root.findViewById(R.id.pv_receipt);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        //tab切换时候的字体设置
        mTabLayout.addOnTabSelectedListener(new SelectedFont(activity));
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        for (TabBean tabBean : tabBeanList) {
            fragments.add(CompanyEvaluationListFragment.newInstance(tabBean.getType(), logistics_id));
        }
        mViewPage.setOffscreenPageLimit(fragments.size());
        mViewPage.setAdapter(new FragmentAdapter(activity, fragments));
        new TabLayoutMediator(mTabLayout, mViewPage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabBeanList.get(position).getTypeName());
            }
        }).attach();
    }


    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(CompanyInfo event) {

        int commentNum = Integer.parseInt(event.getComment_num()) + 1;
        mTvScore.setText(MyUtils.format2(Double.parseDouble(event.getGrade_score()) / commentNum));
        mTvScoreGrade.setText(MyUtils.getScoreGrade(Float.parseFloat(event.getGrade_score()) / commentNum));
        double service = Double.parseDouble(event.getService());
        mTvService.setText(MyUtils.format2(service / commentNum));
        mPvService.setProgress((int) service/commentNum * 2 * 10);
        double waybill = Double.parseDouble(event.getWaybill());
        mTvWaybill.setText(MyUtils.format2(waybill / commentNum));
        mPvWaybill.setProgress((int) waybill/commentNum * 2 * 10);
        double transport = Double.parseDouble(event.getTransport());
        mTvTransport.setText(MyUtils.format2(transport / commentNum));
        mPvTransport.setProgress((int) transport/commentNum * 2 * 10);
        double receipt = Double.parseDouble(event.getReceipt());
        mTvReceipt.setText(MyUtils.format2(receipt / commentNum));
        mPvReceipt.setProgress((int) receipt/commentNum * 2 * 10);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}